package com.iwhalecloud.bss.kite.cucc.service.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.JsonUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.ztesoft.zsmart.core.log.ZSmartLogger;

import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.Objects;

public class PayUtil {

    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(PayUtil.class);

    /**
     * 加密，转换大写
     * @param obj
     * @return
     * @throws Exception
     */
    public static String getEncryptContentForHump(Object obj) throws Exception {
        DcPublic dcPublic = DcPublicCache.get("2021061091", "PAY_KEY_CONFIG");
        if (Objects.isNull(dcPublic)) {
            ErrorConsts.IS_EMPTY.throwOut("", "", "[PAY_KEY_CONFIG]密钥配置信息为空！");
        }
        // 转换大写
        Map<String, Object> beanMap = HumpToLine(obj);
        return UpcEncodeUtilForJson.publicEncode(JSON.toJSONString(beanMap), dcPublic.getCodea(), dcPublic.getCodeb());
    }

    /**
     * 加密，不需要转换大写
     * @param obj
     * @return
     * @throws Exception
     */
    public static String getEncryptContent(Object obj) throws Exception {
        DcPublic dcPublic = DcPublicCache.get("2021061091", "PAY_KEY_CONFIG");
        if (Objects.isNull(dcPublic)) {
            ErrorConsts.IS_EMPTY.throwOut("", "", "[PAY_KEY_CONFIG]密钥配置信息为空！");
        }
        return UpcEncodeUtilForJson.publicEncode(JSON.toJSONString(obj), dcPublic.getCodea(), dcPublic.getCodeb());
    }

    /**
     * 解密
     * @param content
     * @return
     * @throws Exception
     */
    public static String getDecryptContent(String content) throws Exception {
        DcPublic dcPublic = DcPublicCache.get("2021061091", "PAY_KEY_CONFIG");
        if (Objects.isNull(dcPublic)) {
            ErrorConsts.IS_EMPTY.throwOut("", "", "[PAY_KEY_CONFIG]密钥配置信息为空！");
        }
        return UpcEncodeUtilForJson.privateDecode(content, dcPublic.getCodec());
    }

    /**
     * 签名串校验
     * @param decryptContent
     * @return
     * @throws Exception
     */
    public static boolean signValid(String decryptContent) throws Exception {
        DcPublic dcPublic = DcPublicCache.get("2021061091", "PAY_KEY_CONFIG");
        if (Objects.isNull(dcPublic)) {
            ErrorConsts.IS_EMPTY.throwOut("", "", "[PAY_KEY_CONFIG]密钥配置信息为空！");
        }
        return UpcEncodeUtilForJson.signValid(decryptContent, dcPublic.getCodeb());
    }
    /**
     * 驼峰转下划线
     * @param object
     * @return
     */
    public static Map<String, Object> HumpToLine(Object object) {
        return JSONObject.parseObject(JsonUtils.transKeyToUpperCaseObject(JSONObject.toJSON(object).toString()).toJSONString(), Map.class);
    }

    /**
     * Double处理小数点
     * @param d
     * @return
     */
    public static String doubleTrans(double d){
        if(Math.abs(Math.round(d) - d) < 0.000001){
            return String.valueOf((long)d);
        }
        return String.valueOf(d);
    }

    public static String getNextSequence(@NotNull String orderId) {
        StringBuffer sequence = new StringBuffer();
        if (orderId.startsWith("XS")) {
            // 切分
            int length = orderId.length();
            String startSeq = orderId.substring(0, length - 2);
            String endSeq = orderId.substring(length - 2, length);
            sequence.append(startSeq).append(autoGenericCode(endSeq, 2));
        } else {
            sequence.append("XS").append(orderId).append("01");
        }
        return sequence.toString();
    }

    private static String autoGenericCode(String code, int num) {
        return String.format("%0" + num + "d", Integer.parseInt(code) + 1);
    }

}
